import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/home/search_logic.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/base/qkd_dialog.dart';
import 'package:sports_new/widgets/select_bottomsheet.dart';

import '../widgets/base/qkd_text.dart';

class WidgetUtils {
  static Future<bool?> alertQuery(String msg) {
    return Get.dialog(QkdDialog.alert(msg));
  }
}

extension FontWeightEx1 on FontWeight {
  static FontWeight get qxbBold => FontWeight.w600;
  static FontWeight get qxbNormal => FontWeight.w400;
  static FontWeight get qxbMedium => FontWeight.w500;
}

extension StateEx1 on State {
  update() {
    if (!mounted) {
      return;
    }
    setState(() {});
  }
}

extension TextWidth on QkdText {
  Container wrapTextWidth(double fontSize, FontWeight fontWeight) {
    var tp = TextPainter(
        textDirection: TextDirection.ltr,
        text: TextSpan(
            text: data,
            style: TextStyle(fontSize: fontSize, fontWeight: fontWeight)))
      ..layout();
    return Container(width: tp.width, child: this);
  }
}

extension WidgetAlign on Widget {
  Widget get alignEnd =>
      Container(alignment: Alignment.centerRight, child: this);
  Widget get alignStart =>
      Container(alignment: Alignment.centerLeft, child: this);
  Widget get alignTop => Container(alignment: Alignment.topCenter, child: this);
  Widget get alignBottom =>
      Container(alignment: Alignment.bottomCenter, child: this);
}

extension WidgetEx1 on Widget {
  SizedBox sized({double? width = null, double? height = null}) =>
      SizedBox(width: width, height: height, child: this);
  GestureDetector tap(void Function()? fn,
          {HitTestBehavior behavior = HitTestBehavior.opaque}) =>
      GestureDetector(onTap: fn, behavior: behavior, child: this);
  rounded(double r) => Container(
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(r)),
      clipBehavior: Clip.hardEdge,
      child: this);
  Center get center => Center(child: this);

  Widget badge(String? text,
          {bool border = false, Color? color, Color? textColor}) =>
      Stack(
        clipBehavior: Clip.none,
        children: [
          this,
          if ((text ?? "").isNotEmpty)
            Positioned(
                top: 5,
                right: 0,
                width: 1,
                height: 1,
                child: OverflowBox(
                  maxWidth: double.infinity,
                  maxHeight: double.infinity,
                  child: Container(
                    alignment: Alignment.center,
                    height: 13,
                    // width: 12,
                    constraints: const BoxConstraints(minWidth: 12),
                    padding:
                        const EdgeInsets.symmetric(horizontal: 2, vertical: 0),
                    decoration: BoxDecoration(
                        color: color ?? Colours.main,
                        borderRadius: BorderRadius.circular(6.5),
                        border: border
                            ? Border.all(color: Colors.white, width: 1)
                            : null),
                    // height: 11,
                    child: QkdText(
                      text!,
                      needCenter: true,
                      color: textColor ?? Colors.white,
                      fontSize: 10
                    ),
                  ),
                ))
        ],
      );
}

extension TextHighLight on QkdText{
  Widget formHighLight(){
    if(Get.currentRoute != Routes.search){
      return this;
    }
    if(Get.currentRoute == Routes.search && Get.find<SearchLogic>().goSearch != true){
      return this;
    }
    String word = Get.find<SearchLogic>().searchWord;
    var i=0;
    var wordOrder = 0;
    List<String> list = [];
    if(word.hasValue && data.hasValue){
      List<String> input = data!.split("");
      while(i != -1) {
        i = input.indexWhere((element) => word[wordOrder].toLowerCase() == element.toLowerCase());
        if (i != -1) {
          wordOrder += 1;
          if (wordOrder == word.length) wordOrder = 0;
          if (i != 0) {
            list.add(input.sublist(0, i).join());
          }
          list.add(input.sublist(i, i + 1).join());
          input = input.sublist(i + 1, input.length);
        }
      }
      list.add(input.join());
    }
    if(list.length == 1){
      return this;
    }else{
      return RichText(
        textAlign: textAlign ?? TextAlign.start,
        strutStyle: needCenter?Styles.centerStyle(fontSize: innerStyle.fontSize):null,
        softWrap: true,
        overflow: TextOverflow.ellipsis,
        text: TextSpan(
          style: innerStyle,
          children: list.map(
            (e) => TextSpan(
              text: e,
              style: innerStyle.copyWith(
                color: word.toLowerCase().contains(e.toLowerCase())?Colours.main:null)
            )
          ).toList()
        )
      );
    }
  }
}

extension TextSpanHighLight on TextSpan{
  TextSpan formHighLight(){
    if(Get.currentRoute != Routes.search){
      return this;
    }
    if(Get.currentRoute == Routes.search && Get.find<SearchLogic>().goSearch != true){
      return this;
    }
    String word = Get.find<SearchLogic>().searchWord;
    var i=0;
    var wordOrder = 0;
    List<String> list = [];
    if(word.hasValue && text.hasValue){
      List<String> input = text!.split("");
      while(i != -1) {
        i = input.indexWhere((element) => word[wordOrder].toLowerCase() == element.toLowerCase());
        if (i != -1) {
          wordOrder += 1;
          if (wordOrder == word.length) wordOrder = 0;
          if (i != 0) {
            list.add(input.sublist(0, i).join());
          }
          list.add(input.sublist(i, i + 1).join());
          input = input.sublist(i + 1, input.length);
        }
      }
      list.add(input.join());
    }
    if(list.length == 1){
      return this;
    }else{
      return TextSpan(
        style: style,
        children: list.map(
          (e) => TextSpan(text: e,style: TextStyle(color: word.toLowerCase().contains(e.toLowerCase())?Colours.main:null))
        ).toList()
    );
    }
  }
}
